/**
 * Copyright (c) 2022 www.talkweb.com.cn Inc. AlL rights reserved.
 */
package com.sky.wsp.mybatis.plus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.wsp.mybatis.plus.dao.entity.UserEntity;
import com.sky.wsp.mybatis.plus.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户表控制器，使用MybatisPlus自带的函数操作数据库
 *
 * @author wangshaopeng@talkweb.com.cn
 * @Date 2023-01-11
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userServiceImpl")
    private IUserService userService;

    /**
     * 获取用户表分页
     */
    @GetMapping(name = "获取用户表分页", value = "/page")
    public Page<UserEntity> findPage(UserEntity query) {
        Page<UserEntity> page = new Page<>();
        Page<UserEntity> result = userService.findPage(query, page);
        return result;
    }

    /**
     * 获取用户表列表
     */
    @GetMapping(name = "获取用户表列表", value = "/list")
    public List<UserEntity> findList(UserEntity query) {
        List<UserEntity> list = userService.findList(query);
        return list;
    }

    /**
     * 用户表详情
     */
    @GetMapping(name = "获取用户表详情", value = "/detail")
    public UserEntity detail(Long id) {
        UserEntity entity = userService.getById(id);
        if (null == entity) {
            return new UserEntity();
        }
        return entity;
    }

    /**
     * 用户表详情
     */
    @GetMapping(name = "获取用户表详情", value = "/by-mobile")
    public UserEntity byMobile(String mobile) {
        UserEntity entity = userService.getByMobile(mobile);
        if (null == entity) {
            return new UserEntity();
        }
        return entity;
    }

    @GetMapping(name = "查询一百万条单表数据", value = "/s")
    public String s() {
        long begin = System.currentTimeMillis();
        List<UserEntity> batch = userService.list();
        System.out.println(batch.size());
        System.out.println(System.currentTimeMillis() - begin);
        return "ok: " + batch.size();
    }

    @GetMapping(name = "一百万测试数据", value = "/t")
    public String t() {
        for (long i = 0L; i < 9999L; i++) {
            List<UserEntity> batch = new ArrayList<>(100);
            for (long j = 0L; j < 100L; j++) {
                Long id = i * j;
                UserEntity u = new UserEntity();
                u.setTenantId(id);
                u.setOrgId(id);
                u.setRoleId(id);
                u.setName("name" + id);
                u.setAge(1);
                u.setMobile("18888888888");
                batch.add(u);
            }
            userService.saveBatch(batch);
        }

        return "ok";
    }

    /**
     * 新增用户表
     */
    @GetMapping(name = "新增用户表", value = "/add")
    public UserEntity add(UserEntity entity) {
        userService.save(entity);
        return entity;
    }

    /**
     * 修改用户表
     */
    @GetMapping(name = "修改用户表", value = "/update")
    public UserEntity update(UserEntity entity) {
//        userService.updateById(entity);

//        LambdaUpdateWrapper<UserEntity> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.eq(UserEntity::getId, entity.getId());
//        wrapper.set(UserEntity::getMobile, entity.getMobile());
//        wrapper.set(UserEntity::getName, entity.getName());
//        wrapper.eq(UserEntity::getMobile, entity.getMobile());
//        userService.update(wrapper);

        entity.setUpdateTime(new Date());
        entity.setOrgId(1L);
        entity.setAge(18);
        userService.updateById(entity);
        return entity;
    }

    /**
     * 删除用户表
     */
    @GetMapping(name = "删除用户表", value = "/delete")
    public boolean delete(Long id) {
        boolean delete = userService.delete(id);
        return delete;
    }
}
